<?php

/*
 * This file is part of the yuki package.
 * Copyright (c) 2011 olamedia <olamedia@gmail.com>
 *
 * Licensed under The MIT License
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * yBuffer
 *
 * @package yuki
 * @subpackage output
 * @author olamedia
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @version SVN: $Id: yBuffer.php 149 2011-02-20 06:58:24Z olamedia@gmail.com $
 */
class yBuffer{
    protected $_data = '';
    protected $_isStarted = false;
    public function start(){
        if (!$this->_isStarted){
            $this->reset();
            ob_start(array($this, "handler"), 1024);
            $this->_isStarted = true;
        }
    }
    public function reset(){
        $this->_data = '';
        if ($this->_isStarted){
            $this->endClean();
        }
    }
    public function handler($data, $flags = 0){
        $this->_data .= $data;
        /*if ($flags & PHP_OUTPUT_HANDLER_END){
            return '';
        }*/
        return '';
    }
    public function getContents(){
        // already passed to handler + still in ob:
        return $this->_data.ob_get_contents();
    }
    public function getClean(){
        if ($this->_isStarted){
            ob_end_clean();
            $this->_isStarted = false;
        }
        return $this->_data;
    }
    public function endClean(){
        if ($this->_isStarted){
            ob_end_clean();
            $this->_isStarted = false;
        }
        $this->reset();
    }
}

